<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- 分苹果（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>A、B两个人把苹果分为两堆&#xff0c;A希望按照他的计算规则等分苹果&#xff0c;他的计算规则是按照二进制加法计算&#xff0c;并且不计算进位 12&#43;5&#61;9&#xff08;1100 &#43; 0101 &#61; 9&#xff09;&#xff0c;B的计算规则是十进制加法&#xff0c;包括正常进位&#xff0c;B希望在满足A的情况下获取苹果重量最多。</p> 
<p>输入苹果的数量和每个苹果重量&#xff0c;输出满足A的情况下B获取的苹果总重量。</p> 
<p>如果无法满足A的要求&#xff0c;输出-1。</p> 
<p><strong>数据范围</strong></p> 
<p>1 &lt;&#61; 总苹果数量 &lt;&#61; 20000</p> 
<p>1 &lt;&#61; 每个苹果重量 &lt;&#61; 10000</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>输入第一行是苹果数量&#xff1a;3</p> 
<p>输入第二行是每个苹果重量&#xff1a;3 5 6</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>输出第一行是B获取的苹果总重量&#xff1a;11</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">3<br /> 3 5 6</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">11</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">8<br /> 7258 6579 2602 6716 3050 3564 5396 1773</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">35165</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">无</td></tr></tbody></table> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题就是<a href="https://blog.csdn.net/qfc_128220/article/details/127711241?spm&#61;1001.2014.3001.5501" title="华为机试 - 分积木_伏城之外的博客-CSDN博客">华为机试 - 分积木_伏城之外的博客-CSDN博客</a></p> 
<p>题解可以看链接博客</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    const n &#61; parseInt(lines[0]);
    const arr &#61; lines[1].split(&#34; &#34;).map(Number);

    console.log(getResult(n, arr));

    lines.length &#61; 0;
  }
});

function getResult(n, arr) {
  const min &#61; arr.sort((a, b) &#61;&gt; a - b).shift();

  let fault, correct;
  fault &#61; correct &#61; min;

  for (let w of arr) {
    fault ^&#61; w;
    correct &#43;&#61; w;
  }

  if (fault &#61;&#61;&#61; 0) {
    return correct - min;
  } else {
    return -1;
  }
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    int n &#61; sc.nextInt();
    int[] arr &#61; new int[n];
    for (int i &#61; 0; i &lt; n; i&#43;&#43;) {
      arr[i] &#61; sc.nextInt();
    }

    System.out.println(getResult(n, arr));
  }

  public static int getResult(int n, int[] arr) {
    Arrays.sort(arr);

    int min &#61; arr[0];
    int fault &#61; min, correct &#61; min;

    for (int i &#61; 1; i &lt; arr.length; i&#43;&#43;) {
      int w &#61; arr[i];
      fault ^&#61; w;
      correct &#43;&#61; w;
    }

    if (fault &#61;&#61; 0) {
      return correct - min;
    } else {
      return -1;
    }
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
n &#61; int(input())
arr &#61; list(map(int, input().split()))


# 算法入口
def getResult():
    arr.sort()
    fault &#61; correct &#61; minV &#61; arr[0]

    for i in range(1, len(arr)):
        w &#61; arr[i]
        fault ^&#61; w
        correct &#43;&#61; w

    if fault &#61;&#61; 0:
        return correct - minV
    else:
        return -1


# 算法调用
print(getResult())
</code></pre> 
<p> </p> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

void getResult(int n, int weights[]);

int main() {
    int n;
    scanf(&#34;%d&#34;, &amp;n);

    int nums[n];
    for(int i&#61;0; i&lt;n; i&#43;&#43;) {
        scanf(&#34;%d&#34;, &amp;nums[i]);
    }

    getResult(n, nums);

    return 0;
}

int cmp(const void* a, const void* b) {
    return (*(int*) a) - (*(int*) b);
}

void getResult(int n, int nums[]) {
    qsort(nums, n, sizeof(int), cmp);

    int min &#61; nums[0];

    int correctSum &#61; min;
    int faultSum &#61; min;

    for(int i&#61;1; i&lt;n; i&#43;&#43;) {
        correctSum &#43;&#61; nums[i];
        faultSum ^&#61; nums[i];
    }

    if(faultSum &#61;&#61; 0) {
        printf(&#34;%d\n&#34;, correctSum - min);
    } else {
        puts(&#34;-1&#34;);
    }
}</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>